package PHS;
import java.sql.*;
import java.io.*;
public class Patient implements Serializable{
    String url="jdbc:db2://localhOst:50000/PHSYSTEM";
    String driver="com.ibm.db2.jcc.DB2Driver";
    String user="db2admin";
    String pwd="db2admin";
    int rowcount;
    PreparedStatement pstmt;
    Statement stmt;
    Connection conn=null;
    ResultSet rs;
    int patientId;
    String pname;
    String fname;
    int age;
    String gender;
    String mstatus;
    double mnumber;
    String email;
    String symptoms;
    String condition;
    String address;
    public Patient()
    {
        try{
        Class.forName(driver);
        conn=DriverManager.getConnection(url,user,pwd);
        stmt=conn.createStatement();
        System.out.println("connection :"+conn);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    public void setPname(String pname)
    {
        this.pname=pname;
        System.out.println("Setter method for pname:"+this.pname);
    }
    public void setFname(String fname)
    {
        this.fname=fname;
        System.out.println("Setter method for fname:"+this.fname);
    }
    public void setAge(int age)
    {
        this.age=age;
        System.out.println("Setter method for age:"+this.age);
    }
    public void setMstatus(String mstatus)
    {
        this.mstatus=mstatus;
        System.out.println("Setter method for mstatus:"+this.mstatus);
    }
    public void setGender(String gender)
    {
        this.gender=gender;
        System.out.println("Setter method for gender:"+this.gender);
    }
    public void setMnumber(double mnumber)
    {
        try{
        this.mnumber=mnumber;
        System.out.println("Setter method for mobile:"+this.mnumber);
        }
        catch(Exception e)
        {
            
        }
    }
    public void setAddress(String address)
    {
        this.address=address;
        System.out.println("Setter method for address:"+this.address);
    }
    public void setEmail(String email)
    {
        this.email=email;
        System.out.println("Setter method for email:"+this.email);
    }
    public void setSymptoms(String symptoms)
    {
        this.symptoms=symptoms;
        System.out.println("Setter method for symptoms:"+this.symptoms);
    }

    public void setCondition(String condition)
    {
        this.condition=condition;
        System.out.println("Setter method for conditin:"+this.condition);
    }
    public int newPatientInsert()
    {
        String queryPID="SELECT MAX(PatientID) PatientId FROM PatientPersonal";
        String insert="INSERT INTO PATIENTPERSONAL(PatientId,PName,FRHname,Age,Gender,MaritualStatus,mbileNO,EmailID,Address)" +
                " VALUES(?,?,?,?,?,?,?,?,?)";
     
       try{    
        rs=stmt.executeQuery(queryPID);
        while(rs.next())
        {
            patientId=rs.getInt("PatientId");
        }
        patientId++;
         conn.setAutoCommit(false);
        pstmt=conn.prepareStatement(insert);
        pstmt.setInt(1,patientId);
        pstmt.setString(2,pname.toUpperCase());
        pstmt.setString(3,fname.toUpperCase());
        pstmt.setInt(4,age);
        pstmt.setString(5,gender.toUpperCase());
        pstmt.setString(6,mstatus.toUpperCase());
        pstmt.setDouble(7,mnumber);
        pstmt.setString(8,email);
        pstmt.setString(9,address);
        pstmt.executeUpdate();
     String  insert1="INSERT INTO PATIENTTREATMENTDETAILS(PatientId,Symptoms,Condition,Status) VALUES (?,?,?,?)";
        pstmt=conn.prepareStatement(insert1);
        pstmt.setInt(1,patientId);
        pstmt.setString(2,symptoms);
        pstmt.setString(3,condition);
        pstmt.setString(4,"WAIT_ASSIGN");
        pstmt.executeUpdate();
        conn.commit();
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        return patientId;

    }

}
